package com.apache.uct.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.uct.common.entity.ActGive;
import com.apache.uct.manager.ActGiveManager;

public class ActGiveManagerImpl implements ActGiveManager {

	protected IDao actGiveDao;
	protected final String entityName = "com.apache.uct.common.entity.ActGive";

	public void setActGiveDao(IDao actGiveDao) {
		this.actGiveDao = actGiveDao;
	}

	public String saveInfo(ParamsVo<ActGive> vo) throws BusinessException {
		ActGive actGive = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="actGive_"+infoId;
		actGive.setGiveId(infoId);
		MethodParam param = new MethodParam("ActGive", cacheKey, "", entityName);
		param.setVaule(actGive);
		if (actGiveDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<ActGive> vo) throws BusinessException {
		ActGive actGive = vo.getObj();
		if (Validator.isNotNull(actGive.getGiveId())) {
			String cacheKey = "";
			//String cacheKey="actGive_"+actGive.getGiveId();
			MethodParam param = new MethodParam("ActGive", cacheKey, "", entityName);
			param.setVaule(actGive);
			return actGiveDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<ActGive> vo) throws BusinessException {
		String key = vo.getKey();
		MethodParam param = null;
		if (Validator.isNull(key)) {
			ActGive actGive = vo.getObj();
			if (Validator.isNotNull(actGive.getActId()) && Validator.isNotNull(actGive.getRoleId())) {
				param = new MethodParam("ByActIdAndObjId", "", actGiveDao.getSql(4), entityName);
				param.setParams("roleId", actGive.getRoleId());
				param.setParams("actId", actGive.getActId());
				param.setDelete(true);
			} else if (Validator.isNotNull(actGive.getGiveType()) && Validator.isNotNull(actGive.getRoleId())) {//按类型与资源ID
				param = new MethodParam("ByGiveTypeAndObjId", "", actGiveDao.getSql(3), entityName);
				param.setParams("roleId", actGive.getRoleId());
				param.setParams("giveType", actGive.getGiveType());
				param.setDelete(true);
			} else if (Validator.isNotNull(actGive.getActId())) {//按权限ID删除
				param = new MethodParam("ByActId", "", actGiveDao.getSql(5), entityName);
				param.setParams("actId", actGive.getActId());
				param.setDelete(true);
			}
		} else {
			String sysEname = String.valueOf(vo.getParams("sysEname"));
			param = new MethodParam("BySysEname", "", actGiveDao.getSql(6), entityName);
			param.setParams("sysEname", sysEname);
			param.setDelete(true);
		}
		return actGiveDao.delete(param);
	}

	public Object getInfoById(ParamsVo<ActGive> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="actGive_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return actGiveDao.selectById(param);
	}

	public Object execute(ParamsVo<ActGive> vo) {
		String key = vo.getKey();
		if ("GiveListByOrgId".equals(key)) {
			String roleId = String.valueOf(vo.getParams("roleId"));
			String giveType = String.valueOf(vo.getParams("giveType"));
			return getGiveListByOrgIds(roleId, giveType);
		}
		return null;
	}

	//按资ID获取权限列表
	private List<ActGive> getGiveListByOrgIds(String roleId, String giveType) {
		if (Validator.isNull(roleId) || Validator.isNull(giveType))
			return null;
		String hql = actGiveDao.getSql(2) + " and roleId=:roleId and giveType='" + giveType + "'";
		MethodParam param = new MethodParam("ByProperty", "", hql, entityName);
		param.setParams("roleId", roleId);
		param.setParams("giveType", giveType);
		return actGiveDao.select(param);
	}

	public Page getPageInfo(ParamsVo<ActGive> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = actGiveDao.pageSelect(param);
		return page;
	}

	public List<ActGive> getList(ParamsVo<ActGive> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return actGiveDao.select(param);
	}

	public long countInfo(ParamsVo<ActGive> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return actGiveDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<ActGive> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(actGiveDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		ActGive actGive = vo.getObj();
		if (Validator.isNotNull(actGive.getSysEname())) {
			sb.append(" and sysEname =:sysEname");
			param.setParams("sysEname", actGive.getSysEname());
		}
		if (Validator.isNotNull(actGive.getGiveType())) {
			sb.append(" and giveType =:giveType");
			param.setParams("giveType", actGive.getGiveType());
		}
		if (Validator.isNotNull(actGive.getActId())) {
			sb.append(" and actId =:actId");
			param.setParams("actId", actGive.getActId());
		}
		if (Validator.isNotNull(actGive.getActFullEname())) {
			sb.append(" and actFullEname =:actFullEname");
			param.setParams("actFullEname", actGive.getActFullEname());
		}
		if (Validator.isNotNull(actGive.getRoleId())) {
			sb.append(" and roleId =:roleId");
			param.setParams("roleId", actGive.getRoleId());
		}
		if (Validator.isNotNull(actGive.getRoleEname())) {
			sb.append(" and roleEname =:roleEname");
			param.setParams("roleEname", actGive.getRoleEname());
		}
		if (Validator.isNotNull(actGive.getUserId())) {
			sb.append(" and userId =:userId");
			param.setParams("userId", actGive.getUserId());
		}
		if (Validator.isNotNull(actGive.getUserEname())) {
			sb.append(" and userEname =:userEname");
			param.setParams("userEname", actGive.getUserEname());
		}
		if (Validator.isNotNull(actGive.getTmpFlag())) {
			sb.append(" and tmpFlag =:tmpFlag");
			param.setParams("tmpFlag", actGive.getTmpFlag());
		}
		if (Validator.isNotNull(actGive.getGiveRemark())) {
			sb.append(" and giveRemark =:giveRemark");
			param.setParams("giveRemark", actGive.getGiveRemark());
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
